package com.lovegu.collection.map;

import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author 老顾
 * @title: MapTest
 * @projectName java-restudy
 * @email: 1437594522@qq.com
 * @date 2022/9/24 10:47
 */
public class MapTest {
    public Map<String,String> user;
    @Before
    public void add() {
        user = new HashMap<>();
        user.put("username","laogu");
        user.put("password","laogu666");
    }
    @Test
    public void testIterator(){
        // 拿到一个存有所有entry的set集合。
        // entry就是一个个的节点node
        Set<Map.Entry<String, String>> entries = user.entrySet();

        Iterator<Map.Entry<String, String>> iterator = entries.iterator();
        while (iterator.hasNext()){
            Map.Entry<String, String> next = iterator.next();
            System.out.println(next.getKey());
            System.out.println(next.getValue());
        }
    }
    @Test
    public void testIterator2(){
        // 获取一个含有所有key的set集合，去迭代
        Set<String> keys = user.keySet();
        Iterator<String> iterator = keys.iterator();
        while (iterator.hasNext()){
            String key = iterator.next();
            System.out.println(key);
            System.out.println(user.get(key));
        }
    }
    @Test
    public void testEnhancedFor(){
        for (Map.Entry<String,String> entry : user.entrySet()){
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
    }
}
